<template>
  <div>
    <el-form :inline="true">
      <el-form-item>
        <v-distpicker only-province @selected="onSelected" v-if="goodStockDto.roleType === 1">
        </v-distpicker>
        <el-select v-model="goodStockDto.cityCode" placeholder="请选择地市"
                   v-if="(goodStockDto.roleType === 2 && preOfficeType === '0') || (goodStockDto.roleType === 3 &&
                   preOutsourceType === '1')" @change="onCityChange()">
          <el-option
            v-for="item in addressOption"
            :key="item.code"
            :label="item.name"
            :value="item.code">
          </el-option>
        </el-select>
      </el-form-item>
      <!--网点类型查询-->
      <el-form-item v-if="goodStockDto.roleType === 1 || preOfficeType === '0'">
        <el-select v-model="goodStockDto.pointType" placeholder="请选择网点类型" @change="onPointChange()">
          <el-option
            v-for="item1 in pointTypeOptions"
            :key="item1.id"
            :label="item1.name"
            :value="item1.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item v-if="goodStockDto.pointType === 0 && preOfficeType !== '1'">
        <el-select v-model="goodStockDto.pointId" placeholder="请选择办事处" clearable>
          <el-option
            v-for="item1 in pointOptions"
            :key="item1.id"
            :label="item1.name"
            :value="item1.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item v-if="goodStockDto.pointType === 1 && preOutsourceType !== '0'">
        <el-select v-model="goodStockDto.pointId" placeholder="请选择外包商" clearable>
          <el-option
            v-for="item1 in outsourceOptions"
            :key="item1.id"
            :label="item1.company"
            :value="item1.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input placeholder="YSTSN" v-model="goodStockDto.ystsn" clearable></el-input>
      </el-form-item>

      <el-form-item>
        <el-select v-model="goodStockDto.isReturn" clearable placeholder="返机类型">
          <el-option
            v-for="item1 in isReturnOption"
            :key="item1.id"
            :label="item1.name"
            :value="item1.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="searchDate()">查询</el-button>
      </el-form-item>
      <el-form-item v-if="preRoleType === '1'">
        <el-dropdown @command="handleCommand">
          <el-button type="primary">导入<i class="el-icon-arrow-down el-icon--right"></i>
          </el-button>
          <el-dropdown-menu slot="dropdown">
            <a href="/static/良品库导入模板.xlsx" download="良品库导入模板.xlsx" style="color:#5a5e66;text-decoration: none ">
              <el-dropdown-item command="handleDownload">模板下载</el-dropdown-item>
            </a>
            <el-dropdown-item command="handleImport">数据导入</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="excelExport">数据导出</el-button>
      </el-form-item>
      <el-form-item v-if="preRoleType === '2' || preRoleType === '3'">
        <el-dropdown @command="handleExchange">
          <el-button type="primary">转不良品导入<i class="el-icon-arrow-down el-icon--right"></i>
          </el-button>
          <el-dropdown-menu slot="dropdown">
            <a href="/static/转不良品模板.xlsx" download="转不良品模板.xlsx" style="color:#5a5e66;text-decoration: none ">
              <el-dropdown-item command="handleDownload">转不良品模板下载</el-dropdown-item>
            </a>
            <el-dropdown-item command="handleImport">转不良品导入</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-form-item>
    </el-form>
    <custom-table
      :data="goodStockList"
      :columns="columns"
      :tableLoadingFlag="tableLoadingFlag"
      :operationColumn="operationColumn"
      :pagination="pagination">
    </custom-table>

    <el-dialog title="维修记录" :visible.sync="detailDialogVisible" :close-on-click-modal="false">
      <repair-list
        :ystsn="ystsn">
      </repair-list>
      <div slot="footer">
        <el-button type="primary" @click="onBtnCancel()">取消</el-button>
      </div>
    </el-dialog>
    <!--良品转不良品-->
    <el-dialog title="转不良品" :visible.sync="toBadVisible" width=35% @close="toBadCancel">
      <div text-align="center">
        <el-form :inline="true" ref="toBadForm" :rules="toBadFormRules" :model="toBadDto">
          <el-form-item prop="faultAppearance" label="故障现象 ：">
            <el-select style="width:180px;" v-model="toBadDto.faultAppearance" clearable placeholder="请选择硬件故障现象">
              <el-option
                v-for="item in hardwareFaultAppearance"
                :key="item.id"
                :label="item.appearance"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="backFactory" label="维修工厂 ：">
            <el-select v-model="toBadDto.backFactory" clearable placeholder="请选择返回工厂">
              <el-option
                v-for="item1 in returnFactoryOptions"
                :key="item1.id"
                :label="item1.name"
                :value="item1.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" style="margin-bottom: 5px">
          <el-button type="primary" @click="toBadCancel()">取消</el-button>
          <el-button type="primary" @click="toBadConfirm()">确定</el-button>
        </div>
        <div style="height: 10px">
        </div>
      </div>
    </el-dialog>
    <!--转不良品导入模态框-->
    <el-dialog title="模板导入" :visible.sync="exchangeVisible"
               :close-on-click-modal="false" :before-close="exchangeCancel">
      <div>
        <el-form label-width="80px" ref="exchangeForm" :inline="true">
          <el-form-item style="padding-left: 120px">
            <el-upload
              class="upload-demo"
              :action="uploadUrl"
              :headers="myHeaders"
              name="file"
              :on-error="uploadError"
              :on-success="exchangeSuccess"
              :before-upload="beforeAvatarUpload"
              multiple
              drag
              :limit="1"
              :file-list="fileList">
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em><br/>只能上传xlsx文件，且不超过10M</div>
            </el-upload>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
    <!-- 良品模板导入模态框 -->
    <el-dialog title="模板导入" :visible.sync="uploadVisible"
               :close-on-click-modal="false" :before-close="uploadCancel">
      <div>
        <el-form :model="standbyImportDto" :rules="standbyImportRule" label-width="120px" ref="uploadForm"
                 :inline="true">
          <el-form-item label="网点类型：" prop="pointType">
            <el-radio
              v-model="standbyImportDto.pointType"
              label="0"
              @change="pointTypeChange">办事处
            </el-radio>
            <el-radio
              v-model="standbyImportDto.pointType"
              label="1"
              @change="pointTypeChange">外包商
            </el-radio>
          </el-form-item>
          <el-form-item v-if="standbyImportDto.pointType === '0'" label="办事处：" prop="pointId">
            <el-select v-model="standbyImportDto.pointId">
              <el-option
                v-for="office in allOffice"
                :key="office.id"
                :value="office.id"
                :label="office.name">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item v-if="standbyImportDto.pointType === '1'" label="外包商：" prop="pointId">
            <el-select v-model="standbyImportDto.pointId">
              <el-option
                v-for="outsource in allOutsource"
                :key="outsource.id"
                :value="outsource.id"
                :label="outsource.company">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="选择文件：">
            <el-upload
              class="upload-demo"
              name="file"
              ref="upload"
              accept=".xls,.XLS,.xlsx,.XLSX"
              :action="uploadUrl"
              :headers="myHeaders"
              :data="standbyImportDto"
              :before-upload="beforeAvatarUpload"
              :on-remove="handleRemove"
              :on-success="handleSuccess"
              :on-error="handleError"
              :file-list="fileList"
              :auto-upload="false"
              :multiple="false">
              <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
              <div slot="tip" class="el-upload__tip">只能上传单个excel文件，且不超过10M</div>
            </el-upload>
          </el-form-item>
          <!--显示导入中错误的数据-->
          <div style="height: 200px;overflow-y: auto" v-if="hideTable">
            <custom-table
              :data="wrongDataList"
              :columns="wrongDetailColumns">
            </custom-table>
          </div>
        </el-form>
      </div>
      <div slot="footer">
        <el-button type="primary" @click="submitUpload()">确定</el-button>
        <el-button @click="uploadCancel()">取 消</el-button>
      </div>
    </el-dialog>
    <!--编辑功能模态框-->
    <el-dialog title="编辑良品库设备" :visible.sync="editVisible" :close-on-click-modal="false">
      <div text-align="center">
        <el-form :model="editDto" label-width="120px" ref="editForm">
          <el-form-item label="型号" prop="brand">
            <el-select style="width:180px;" v-model="editDto.modelId" clearable placeholder="请选择型号">
              <el-option
                v-for="item in hardwareModel"
                :key="item.id"
                :label="item.code"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="YSTSN" prop="ystsn">
            <el-input v-model="editDto.ystsn"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="editCancel()">取消</el-button>
        <el-button type="primary" @click="editConfirm()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import CustomTable from '@/components/table/CustomTable'
  import {
    apiGetGoodStockList,
    apiGoodExport,
    apiUpdateGoodToBad,
    apiGetReturnFactory,
    apiUpdateGoodToScrap,
    apiUpdateGoodStockDevice
  } from '@/api/stock/index'
  import {
    apiGetCityListByProvinceCode,
    apiGetFaultAppearance,
    apiGetModelList,
    apiGetAllPointOffice,
    apiGetAllOffice,
    apiGetAllOutsource
  } from '@/api/dictionary'
  import {
    getUserId,
    getUserName,
    getRoleType,
    getOfficeType,
    getCityCode,
    getProvinceCode,
    getOfficeId,
    getOfficeProvinceCode,
    getOfficeCityCode,
    getOutsourceType,
    getOutsourceId,
    getOutsourceProvinceCode,
    getOutsourceCityCode
  } from '@/utils/token'
  import {parseTime} from '@/utils'
  import {baseInfo} from '@/utils/fetch'
  import store from '@/store'
  import {apiGoodStockReadExcel, apiExchangeGoodReadExcel} from '@/api/excel/excelRead'
  import RepairList from '../../../components/table/RepairList.vue'
  import {apiGetOutsourcerList} from '@/api/basic/info/outsourcer'
  import {apiGetOfficeList} from '@/api/basic/info/inside/office'
  import {downloadExcel} from "../../../utils";

  export default {
    components: {
      RepairList,
      CustomTable
    },
    created () {
      this.selectHardWareModel()
      this.selectHardWareFaultAppearance()
      this.selectReturnFactoryInit()
      this.preOfficeType = getOfficeType()
      this.preRoleType = getRoleType()
      this.preOutsourceType = getOutsourceType()
      let listQuery = {
        pageNumber: this.pagination.currentPage,
        pageSize: this.pagination.pageSize
      }
      if (getRoleType() === '2') {
        this.goodStockDto.roleType = 2
        listQuery.province = getOfficeProvinceCode()
        this.goodStockDto.provinceCode = getOfficeProvinceCode()
        if (getOfficeType() === '1') {
          listQuery.city = getOfficeCityCode()
          this.goodStockDto.cityCode = getOfficeCityCode()
        }
        this.selectedAddressInit()
      }
      if (getRoleType() === '3') {
        this.goodStockDto.roleType = 3
        this.goodStockDto.pointType = 1
        this.goodStockDto.provinceCode = getOutsourceProvinceCode()
        listQuery.province = getOutsourceProvinceCode()
        if (getOutsourceType() === '0') {
          listQuery.city = getOutsourceCityCode()
          this.goodStockDto.cityCode = getOutsourceCityCode()
        }
        this.selectedAddressInit()
      }
      this.getGoodStockList(listQuery)
      this.getOffice()
      this.getOutsource()
    },
    data () {
      return {
        uploadUrl: baseInfo() + '/api/v1/excel/good/stock/read',
        myHeaders: {'access_token': store.getters.token},
        // 上传文件显示
        fileList: [],
        // 文件上传路径
        importFileUrl: baseInfo() + '/api/v1/upload',
        // 文件上传参数
        upLoadData: {},
        filePath: '',
        uploadVisible: false,
        exchangeVisible: false,
        editVisible: false,
        toBadVisible: false,
        toScrapVisible: false,
        hideTable: false,
        wrongDetailColumns: [
          {label: '错误行号', name: 'wrongRowNO', width: '77', height: '40', align: 'center'},
          {label: '错误原因', name: 'reason', width: '120', height: '40', align: 'center'},
          {label: '品牌', name: 'brand', align: 'center'},
          {label: '型号', name: 'model', align: 'center'},
          {label: 'YSTSN', name: 'ystsn', align: 'center'}
        ],
        tableLoadingFlag: false,
        // 良品库存一览表
        columns: [
          {label: '品牌', name: 'brand', align: 'center'},
          {label: '型号', name: 'model', width: '100', align: 'center'},
          {label: '省份', name: 'provinceName', align: 'center'},
          {label: '城市', name: 'cityName', align: 'center'},
          {label: 'YSTSN', name: 'ystsn', width: '220', align: 'center'},
          {label: '是否客户机', name: 'isReturn', width: '100', align: 'center'},
          {label: '入库时间', name: 'inStockTime', width: '160', align: 'center', formatter: this.formatTime},
          {label: '保修截止日期', name: 'deadLine', width: '160', align: 'center', formatter: this.formatTime}
        ],
        goodStockList: [],
        isReturnOption: [
          {
            'id': 0,
            'name': '备机'
          },
          {
            'id': 1,
            'name': '客户机'
          }
        ],
        // 办事处
        pointOptions: [],
        // 外包商
        outsourceOptions: [],
        addressOption: [],
        // 获取模型型号
        hardwareModel: [],
        hardwareFaultAppearance: [],
        returnFactoryOptions: [],
        preOfficeType: null,
        preOutsourceType: null,
        goodStockDto: {
          provinceCode: '',
          cityCode: '',
          pointType: 0,
          pointId: '',
          roleType: 1,
          ystsn: '',
          isReturn: '',
          officeType: getOfficeType()
        },
        toBadDto: {
          ystsn: '',
          faultAppearance: '',
          backFactory: ''
        },
        toScrapDto: {
          ystsn: ''
        },
        editDto: {
          modelId: '',
          ystsn: '',
          id: '',
          isReturn: ''
        },
        operationColumn: {
          width: '100',
          operations: [
            {name: '维修病例', callback: this.onBtnRepairHistory},
            {name: '转不良品', callback: this.handleToBad},
            {name: '转报废库', callback: this.handleToScrap},
            {name: '编辑', callback: this.handleEdit, page: 'goodStockEdit'}
          ]
        },
        detailTableLoadingFlag: false,
        detailDialogVisible: false,
        pagination: this.getData(this.getGoodStockList),
        pagination1: this.getData(this.onBtnRepairHistory),
        pointTypeOptions: [
          {
            'id': 0,
            'name': '办事处'
          },
          {
            'id': 1,
            'name': '外包商'
          }
        ],
        preRoleType: getRoleType(),
        ystsn: '',
        toBadFormRules: {
          faultAppearance: [
            {required: true, message: '请选择设备故障类型', trigger: 'change'}
          ],
          backFactory: [
            {required: true, message: '请选择返还工厂', trigger: 'change'}
          ]
        },

        allOffice: [],
        allOutsource: [],
        standbyImportDto: {
          pointType: '',
          pointId: '',
          operator: '',
          userId: ''
        },
        standbyImportRule: {
          pointType: [
            {required: true, message: '请选择网点类型'}
          ],
          pointId: [
            {required: true, message: '请选择网点'}
          ]
        }
      }
    },
    methods: {
      excelExport () {
        let paramsPost = {
          province: this.goodStockDto.provinceCode,
          city: this.goodStockDto.cityCode,
          pointType: this.goodStockDto.pointType,
          pointId: this.goodStockDto.pointId,
          ystsn: this.goodStockDto.ystsn,
          isReturn: this.goodStockDto.isReturn
        }
        if (getRoleType() === '2') {
          paramsPost.province = getOfficeProvinceCode()
          if (getOfficeType() === '1') {
            paramsPost.city = getOfficeCityCode()
          }
        }
        if (getRoleType() === '3') {
          paramsPost.province = getOutsourceProvinceCode()
          if (getOutsourceType() === '0') {
            paramsPost.city = getOutsourceCityCode()
          }
        }
        apiGoodExport(paramsPost).then(response => {
          downloadExcel(response.data, '自有系统良品库存信息导出表')
        }).catch(() => {
          console.log('error')
        })
      },
      uploadCancel () {
        this.uploadVisible = false
        this.wrongDataList = []
        this.fileList = []
        this.hideTable = false
        this.$refs['uploadForm'].resetFields()
      },
      exchangeCancel () {
        this.exchangeVisible = false
        this.fileList = []
        this.$refs['exchangeForm'].resetFields()
      },
      handleCommand (command) {
        if (command === 'handleImport') {
          this.dialogType = 'upload'
          this.uploadVisible = true
        } else if (command === 'handleDownload') {
        }
      },
      handleExchange (command) {
        if (command === 'handleImport') {
          this.exchangeVisible = true
        } else if (command === 'handleDownload') {
        }
      },
      uploadSuccess (response, file, fileList) {
        this.$message({
          showClose: true,
          message: '上传文件成功!'
        })
        let filePath = response.data
        this.excelRead(filePath)
      },
      exchangeSuccess (response, file, fileList) {
        this.$message({
          showClose: true,
          message: '上传文件成功!'
        })
        let filePath = response.data
        this.excelReadExchange(filePath)
      },
      // 上传错误
      uploadError (err, file, fileList) {
        this.$message({
          showClose: true,
          message: '上传失败，请重试！'
        })
      },
      // 上传前对文件的大小的判断
      beforeAvatarUpload (file) {
        const extension = file.name.split('.')[1] === 'xlsx'
        const isLt2M = file.size / 1024 / 1024 < 10
        if (!extension) {
          this.$message({
            showClose: true,
            message: '上传模板只能是 xlsx 格式!'
          })
        }
        if (!isLt2M) {
          this.$message({
            showClose: true,
            message: '上传模板大小不能超过 10MB!'
          })
        }
        return extension && isLt2M
      },
      excelReadExchange (filePath) {
        let listQuery = {
          filePath: filePath
        }
        apiExchangeGoodReadExcel(listQuery).then(response => {
          this.$message({
            showClose: true,
            message: '导入执行中，请稍后',
            type: 'success'
          })
          if (!response.data) {
            this.$message({
              showClose: true,
              message: '导入成功',
              type: 'success'
            })
            this.getGoodStockList({
              pageNumber: this.pagination.currentPage,
              pageSize: this.pagination.pageSize
            })
          } else if (response.data.message) {
            this.$message({
              showClose: true,
              message: response.data.message,
              type: 'error',
              duration: 0
            })
          }
        })
      },
      excelRead (filePath) {
        let listQuery = {
          filePath: filePath
        }
        apiGoodStockReadExcel(listQuery).then(response => {
          if (!response.data) {
            this.$message({
              showClose: true,
              message: '导入执行中，请稍后',
              type: 'success'
            })
            this.hideTable = false
            this.getGoodStockList({
              pageNumber: this.pagination.currentPage,
              pageSize: this.pagination.pageSize
            })
          } else if (response.data.message) {
            this.$message({
              showClose: true,
              message: response.data.message,
              type: 'error',
              duration: 0
            })
          } else if (response.data) {
            this.wrongDataList = response.data
            this.hideTable = true
          }
          this.fileList = []
        })
      },
      // 地址初始化
      selectedAddressInit () {
        let listquery = {
          province: ''
        }
        if (this.goodStockDto.roleType === 2) {
          listquery.province = getOfficeProvinceCode()
        } else if (this.goodStockDto.roleType === 3) {
          listquery.province = getOutsourceProvinceCode()
        }
        apiGetCityListByProvinceCode(listquery).then(response => {
          let cityList = response.data
          let city = []
          let temp = {}
          for (let i in cityList) {
            if (!temp[cityList[i].name]) {
              temp[cityList[i].name] = cityList[i].code
            }
          }
          city.push({'name': '全部地市', 'code': ''})
          for (let k in temp) {
            city.push({'name': k, 'code': temp[k]})
          }
          this.addressOption = city
        }).catch(() => {
          this.tableLoadingFlag = false
        })
      },
      selectHardWareFaultAppearance () {
        let listQuery = {
          faultType: 1
        }
        apiGetFaultAppearance(listQuery).then(response => {
          this.hardwareFaultAppearance = response.data
        })
      },
      selectReturnFactoryInit () {
        apiGetReturnFactory().then(response => {
          let factoryList = response.data
          let factory = []
          let temp = {}
          for (let i in factoryList) {
            if (!temp[factoryList[i].name]) {
              temp[factoryList[i].name] = factoryList[i].id
            }
          }
          for (let k in temp) {
            factory.push({'name': k, 'id': temp[k]})
          }
          this.returnFactoryOptions = factory
        }).catch(() => {
        })
      },
      getGoodStockList (listQuery) {
        if (listQuery.province === undefined) {
          if (this.goodStockDto.provinceCode === '') {
            if (getRoleType() === '2') {
              listQuery.province = getOfficeProvinceCode()
              if (getOfficeType() === '1') {
                listQuery.city = getOfficeCityCode()
              } else {
                listQuery.city = this.goodStockDto.cityCode
              }
            }
          } else {
            listQuery.province = this.goodStockDto.provinceCode
          }
        }
        if (!listQuery.pointType) {
          listQuery.pointType = this.goodStockDto.pointType
        }
        if (!listQuery.pointId) {
          listQuery.pointId = this.goodStockDto.pointId
        }
        this.tableLoadingFlag = true
        listQuery.isReturn = this.goodStockDto.isReturn
        listQuery.pageSize = this.pagination.pageSize
        listQuery.pageNumber = this.pagination.currentPage
        listQuery.ystsn = this.goodStockDto.ystsn
        apiGetGoodStockList(listQuery).then(response => {
          this.goodStockList = response.data
          this.pagination.totalRecords = response.totalRecords
          this.tableLoadingFlag = false
        }).catch(() => {
          this.tableLoadingFlag = false
        })
      },
      onSelected (data) {             // 省市区联动回调
        this.goodStockDto.provinceCode = data.province.code
      },
      // 条件查询
      searchDate () {
        let listQuery = {
          pageNumber: 1,
          pageSize: 10,
          province: this.goodStockDto.provinceCode,
          city: this.goodStockDto.cityCode,
          pointType: this.goodStockDto.pointType,
          pointId: this.goodStockDto.pointId,
          ystsn: this.goodStockDto.ystsn,
          isReturn: this.goodStockDto.isReturn
        }
        if (getRoleType() === '2') {
          listQuery.province = getOfficeProvinceCode()
          if (getOfficeType() === '1') {
            listQuery.city = getOfficeCityCode()
          } else {
            listQuery.city = this.goodStockDto.cityCode
          }
        }
        if (getRoleType() === '3') {
          listQuery.province = getOutsourceProvinceCode()
        }
        this.goodStockList = []
        this.pagination.totalRecords = 0
        this.getGoodStockList(listQuery)
      },
      // 弹出维修一览界面
      onBtnRepairHistory (row) {
        this.detailDialogVisible = true
        this.ystsn = row.ystsn
      },
      handleToBad (row) {
        this.toBadVisible = true
        this.toBadDto.ystsn = row.ystsn
      },
      handleToScrap (row) {
        this.$confirm('确定转入报废库？', '转入报废库', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.toScrapDto.ystsn = row.ystsn
          apiUpdateGoodToScrap(this.toScrapDto).then(() => {
            this.$message({
              showClose: true,
              type: 'success',
              message: '转报废品成功!'
            })
            this.searchDate()
          })
        })
      },
      // 关闭弹出框
      onBtnCancel () {
        this.detailTableLoadingFlag = false
        this.tableLoadingFlag = false
        this.detailDialogVisible = false
      },
      toBadCancel () {
        this.toBadVisible = false
        this.$refs['toBadForm'].resetFields()
        this.toBadDto = {
          ystsn: '',
          faultAppearance: '',
          backFactory: ''
        }
      },
      toBadConfirm () {
        this.$refs['toBadForm'].validate((valid) => {
          if (valid) {
            apiUpdateGoodToBad(this.toBadDto).then(() => {
              this.$message({
                showClose: true,
                type: 'success',
                message: '转不良品成功!'
              })
              this.toBadDto = {
                ystsn: '',
                faultAppearance: '',
                backFactory: ''
              }
              this.$refs['toBadForm'].resetFields()
              this.toBadVisible = false
              this.searchDate()
            })
          } else {
            return false
          }
        })
      },
      // 编辑按钮
      handleEdit (row) {
        this.editVisible = true
        this.editDto.ystsn = row.ystsn
        this.editDto.id = row.id
        this.editDto.isReturn = row.isReturn
        this.editDto.modelId = row.modelId
      },
      editCancel () {
        this.editVisible = false
        this.$refs['editForm'].resetFields()
        this.editDto = {
          ystsn: '',
          modelId: ''
        }
      },
      // 确认编辑
      editConfirm () {
        apiUpdateGoodStockDevice(this.editDto).then(() => {
          this.$message({
            showClose: true,
            message: '编辑成功',
            type: 'success'
          })
          this.editCancel()
          this.searchDate()
        })
      },
      // 获取模型型号
      selectHardWareModel () {
        let listQuery = {
          modelId: 1
        }
        apiGetModelList(listQuery).then(response => {
          this.hardwareModel = response.data
        })
      },
      onCityChange () {
        if (this.goodStockDto.pointType === 0) {
          let listQuery = {
            province: this.goodStockDto.provinceCode,
            city: this.goodStockDto.cityCode
          }
          this.pointOptions = []
          this.getOfficeByCity(listQuery)
        } else {
          let listQuery = {
            provinceCode: this.goodStockDto.provinceCode,
            cityCode: this.goodStockDto.cityCode
          }
          this.outsourceOptions = []
          this.getOutsourceByCity(listQuery)
        }
      },
      onPointChange () {
        if (this.goodStockDto.pointType === 0) {
          let listQuery = {
            province: this.goodStockDto.provinceCode,
            city: this.goodStockDto.cityCode
          }
          this.pointOptions = []
          this.getOfficeByCity(listQuery)
        } else {
          let listQuery = {
            provinceCode: this.goodStockDto.provinceCode,
            cityCode: this.goodStockDto.cityCode
          }
          this.outsourceOptions = []
          this.getOutsourceByCity(listQuery)
        }
      },
      // 获取网点一览
      getOffice () {
        let listQuery = {
          provinceCode: this.goodStockDto.provinceCode,
          pageNumber: 1,
          pageSize: 100
        }
        apiGetOfficeList(listQuery).then(response => {
          this.pointOptions = response.data
        })
      },
      // 获取外包商一览
      getOutsource () {
        let listQuery = {
          provinceCode: this.goodStockDto.provinceCode,
          pageNumber: 1,
          pageSize: 100
        }
        apiGetOutsourcerList(listQuery).then(response => {
          this.outsourceOptions = response.data
        })
      },
      // 按地区条件获取网点
      getOfficeByCity (listQuery) {
        this.goodStockDto.pointId = ''
        apiGetAllPointOffice(listQuery).then(response => {
          this.pointOptions = response.data
        })
      },
      // 按地区获取外包商
      getOutsourceByCity (listQuery) {
        this.goodStockDto.pointId = ''
        listQuery.pageNumber = 1
        listQuery.pageSize = 100
        apiGetOutsourcerList(listQuery).then(response => {
          this.outsourceOptions = response.data
        })
      },
      formatTime (row, column, cellValue) {
        if (column.property === 'deadLine') {
          return parseTime(cellValue, '{y}-{m}-{d}')
        } else {
          return parseTime(cellValue)
        }
      },
      // 获取办事处一览
      getAllOffice () {
        apiGetAllOffice().then(resp => {
          this.allOffice = resp.data
        })
      },
      getAllOutsource () {
        apiGetAllOutsource().then(resp => {
          this.allOutsource = resp.data
        })
      },
      pointTypeChange () {
        if (this.standbyImportDto.pointType === '0') {
          this.getAllOffice()
        } else {
          this.getAllOutsource()
        }
      },
      handleRemove (file, fileList) {
        this.fileList = []
      },
      handleError () {
        this.$message.error({
          message: '网络繁忙，文件上传失败',
          showClose: true
        })
      },
      handleSuccess (response, file, fileList) {
        if (!response.data) {
          this.$message({
            showClose: true,
            message: '良品备机导入成功',
            type: 'success'
          })
          this.hideTable = false
          this.uploadCancel()
          this.getGoodStockList({
            pageNumber: this.pagination.currentPage,
            pageSize: this.pagination.pageSize
          })
        } else if (response.data.message) {
          this.$message({
            showClose: true,
            message: response.data.message,
            type: 'error',
            duration: 0
          })
        } else if (response.data) {
          this.wrongDataList = response.data
          this.hideTable = true
        }
        this.fileList = []
      },
      submitUpload () {
        this.$refs.uploadForm.validate(valid => {
          if (valid) {
            this.standbyImportDto.userId = getUserId()
            this.standbyImportDto.operator = getUserName()
            this.$refs.upload.submit()
          } else {
            return false
          }
        })
      },
    }
  }
</script>
